package com.icloud.system.boot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.icloud.system.boot.model.CustomerInvite;
import com.icloud.system.boot.vo.CustomerInvitePathVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CustomerInviteMapper extends BaseMapper<CustomerInvite> {

    @Select("WITH RECURSIVE invite_path AS (" +
" SELECT i.invitee_id, i.inviter_id, 1 path_level" +
" FROM customer_invite i" +
" WHERE invitee_id = #{customerId}" +
" UNION ALL" +
" SELECT i.invitee_id, i.inviter_id, c.path_level+1" +
" FROM customer_invite i" +
" INNER JOIN invite_path c ON c.inviter_id = i.invitee_id" +
")" +
" SELECT invitee_id, inviter_id, path_level" +
" FROM invite_path" +
" WHERE invitee_id is not null" +
" ORDER BY path_level asc")
    List<CustomerInvitePathVo> findInvitePathById(@Param("customerId") Long customerId);
}
